package com.my.a_StackAndQueue.dogCatQueue;

import java.util.LinkedList;
import java.util.Queue;

public class DogCatQueue {
    private Queue<PetEnterQueue> dogq;
    private Queue<PetEnterQueue> catq;
    private long count;
    //构造器
    public DogCatQueue() {
        this.dogq = new LinkedList<PetEnterQueue>();
        this.catq = new LinkedList<PetEnterQueue>();
        this.count = 0;
    }
    //总队列是否为空
    public boolean isEmpty(){
        return (this.dogq.isEmpty()&&this.catq.isEmpty());
    }
    //狗队列是否为空
    public boolean isDogQueueEmpty(){
        return this.dogq.isEmpty();
    }
    //猫队列是否为空
    public boolean isCatQueueEmpty(){
        return this.catq.isEmpty();
    }
    //入队方法
    public void add(Pet pet){
        if (pet.getPetType().equals("dog")){
            this.dogq.add(new PetEnterQueue(pet, this.count++));
        }else if (pet.getPetType().equals("cat")){
            this.catq.add((new PetEnterQueue(pet, this.count++)));
        }else{
            throw new RuntimeException("不是猫或狗！");
        }
    }
    //按照实际顺序弹出队列
    public Pet pollAll(){
        if (!this.dogq.isEmpty() && !this.catq.isEmpty()){
            if (this.dogq.peek().getCount() < this.catq.peek().getCount()){
                return this.dogq.poll().getPet();
            }else{
                return this.catq.poll().getPet();
            }
        }else if (!this.dogq.isEmpty()){
            return this.dogq.poll().getPet();
        }else if (!this.catq.isEmpty()){
            return this.catq.poll().getPet();
        }else{
            throw new RuntimeException("此队列为空！");
        }
    }
    //弹出dog类实例
    public Dog pollDog(){
        if (this.isDogQueueEmpty()){
            return (Dog)this.dogq.poll().getPet();
        }else{
            throw new RuntimeException("狗队列为空！");
        }
    }
    //弹出cat队列
    public Cat pollCat(){
        if (this.isCatQueueEmpty()){
            return (Cat)this.catq.poll().getPet();
        }else {
            throw new RuntimeException("猫队列为空！");
        }
    }
}
